//
//  JKRFeedDetailViewController.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/9.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedDetailViewController.h"
#import "JKRFeedDetailTopView.h"
#import "JKRFeedDetailCommentCountView.h"
#import "JKRFeedDetailCommentCell.h"
#import "JKRFeedDetailToolView.h"



@interface JKRFeedDetailViewController ()<UITableViewDelegate, UITableViewDataSource, JKRFeedDetailCommentCellDelegate, JKRFeedDetailCommentCountViewDelegate, JKRFeedDetailTopViewDelegate, JKRFeedDetailToolViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) JKRFeedDetailTopView *topView;
@property (nonatomic, strong) JKRFeedDetailCommentCountView *commentCountView;
@property (nonatomic, strong) JKRFeedDetailToolView *toolView;

@property (nonatomic, strong) NSMutableArray<JKRFeedCommentLayout *> *commentLayouts;
@property (nonatomic, strong) JKRFeedCommentCountLayout *commentCountLayout;

@end

@implementation JKRFeedDetailViewController

static NSString *const feedCommentCellID = @"feedCommentCellID";

- (instancetype)init {
    self = [super init];
    self.hidesBottomBarWhenPushed = YES;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.feedID = @"109213";
    self.title = @"详情";
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:247/255.0 blue:248/255.0 alpha:1.0];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.toolView];
}

#pragma mark - 评论tableView代理
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.commentLayouts.count) {
        self.commentCountView.layout = self.commentCountLayout;
        return self.commentCountView;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.commentLayouts.count && self.commentCountLayout.height > 0) {
        return self.commentCountLayout.height;
    } else {
        return 0.f;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.commentLayouts.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.commentLayouts[indexPath.row].cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JKRFeedDetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:feedCommentCellID];
    if (!cell) {
        cell = [[JKRFeedDetailCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:feedCommentCellID];
        cell.delegate = self;
    }
    cell.layout = self.commentLayouts[indexPath.row];
    return cell;
}

#pragma mark - 发现详情事件处理
- (void)topViewDidClickUserWithUserID:(NSString *)userID {
    NSLog(@"跳转ID为 <%@> 用户主页", userID);
}

#pragma mark - 评论顶部事件处理
- (void)feedCommentCountViewDidClickUserWithUserID:(NSString *)userID {
    NSLog(@"评论顶部点击的ID为 <%@> 赞用户头像", userID);
}

- (void)feedCommentCountViewDidClickVoteUserList {
    NSLog(@"跳转到点赞用户列表");
}

#pragma mark - 评论事件处理
- (void)feedCommentReplayWithCommentID:(NSString *)commentID {
    NSLog(@"回复ID为 <%@> 的发现中的ID为 <%@> 的评论。", self.feedID, commentID);
}

- (void)feedCommentDidClickUserWithUserID:(NSString *)userID {
    NSLog(@"跳转到ID为 <%@> 的用户的主页", userID);
}

#pragma mark - 工具条事件处理
- (void)toolViewDidClickCommentButton {
    @weakify(self);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSData *data = [NSData dataNamed:@"feed_detail_1.json"];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
        JKRFeedDataModel *model = [JKRFeedDataModel modelWithDictionary:dic[@"data"]];
        JKRFeedDetailLayout *layout = [[JKRFeedDetailLayout alloc] initWithModel:model];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            weak_self.topView.layout = layout;
            weak_self.tableView.tableHeaderView = weak_self.topView;
        });
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSData *data = [NSData dataNamed:@"feed_comment_list.json"];
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
                JKRFeedCommentListModel *commentListModel = [JKRFeedCommentListModel modelWithDictionary:dic];
                [weak_self.commentLayouts removeAllObjects];
                for (JKRFeedCommentModel *commentModel in commentListModel.comments) {
                    JKRFeedCommentLayout *commentLayout = [[JKRFeedCommentLayout alloc] initWithCommentModel:commentModel];
                    [weak_self.commentLayouts addObject:commentLayout];
                }
                weak_self.commentCountLayout = [[JKRFeedCommentCountLayout alloc] initWithModel:commentListModel];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weak_self.tableView reloadData];
                });
            });
            
        });
        
    });
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + (iPhoneX ? 24 : 0), kScreenWidth, kScreenHeight - (iPhoneX ? 58 : 0) - 53 - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithRed:244/255.0 green:247/255.0 blue:248/255.0 alpha:1.0];
    }
    return _tableView;
}

- (JKRFeedDetailTopView *)topView {
    if (!_topView) {
        _topView = [JKRFeedDetailTopView new];
        _topView.delegate = self;
    }
    return _topView;
}

- (JKRFeedDetailCommentCountView *)commentCountView {
    if (!_commentCountView) {
        _commentCountView = [JKRFeedDetailCommentCountView new];
        _commentCountView.delegate = self;
    }
    return _commentCountView;
}

- (NSMutableArray *)commentLayouts {
    if (!_commentLayouts) {
        _commentLayouts = [NSMutableArray array];
    }
    return _commentLayouts;
}

- (JKRFeedDetailToolView *)toolView {
    if (!_toolView) {
        _toolView = [[JKRFeedDetailToolView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 56 - (iPhoneX ? 34 : 0), kScreenWidth, 56)];
        _toolView.delegate = self;
    }
    return _toolView;
}

- (void)dealloc {
    NSLog(@"dealloc");
}

@end
